package com.sendi.system.action;
/*
 * 系统参数配置，对于一些常量进行配置，如系统标题
 */
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONObject;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.BeanUtils;

import com.sendi.system.bean.PreloadBeanconfig;
import com.sendi.system.entity.SysConfigPara;


public class SysConfigParaAction extends SysAction {
	
	private Integer id;
	
	private String paraName;
	private String paraValue;
	private String paraDesc;
 
	  
	public SysConfigParaAction()
	{
		super();
		data_session_name="SysConfigParaActionData";
	}
	
	
	public void sysConfigParaInit()
	{
		List datas=null;
		
		if(getSession().getAttribute(data_session_name)!=null)
		{
			datas=(List)getSession().getAttribute(data_session_name);
			System.out.println("use session size"+datas.size());
		}
		else
		{
			addDataToSession();
			datas=(List)getSession().getAttribute(data_session_name);
		}
		 
			writeJSONArray(datas);
		 
	}
	
	public void addDataToSession()
	{
		List datas=null;
		String hql="from  SysConfigPara ";
	 	datas=serviceSupportImp.findByHQL(hql);
	 	getSession().setAttribute(data_session_name,datas);
	 	System.out.println("data size:"+datas.size());
	}
	
 
	
	 public void addSysConfigPara()
	 {
		 
	 	 SysConfigPara sysConfigPara=new SysConfigPara(paraName,  paraValue,paraDesc);
	 		try {
				serviceSupportImp.save(sysConfigPara);
				getSession().removeAttribute(data_session_name);
				addDataToSession();
				writeResponseText("{success:true,msg:'success'}");
		 	} catch (Exception e) {
				writeResponseText("{success:false,msg:'faild',errors:{"+e.getMessage()+"}}");
			  e.printStackTrace();
			}
	 }

		public void querySysConfigPara()
		{
			   try
			   {
				     Session session=serviceSupportImp.getDao().getSessionFactory().openSession();
				 	StringBuffer hql=new StringBuffer("from SysConfigPara where 1=1 ");
					if(this.paraDesc!=null&&paraDesc.trim().length()>0)
					{
						hql.append(" and  paraDesc like :paraDesc ");
					}
					if(this.paraName!=null&&paraName.trim().length()>0)
					{
						hql.append(" and  paraName like :paraName ");
					}
					   Query query= session.createQuery(hql.toString());
					   if(paraDesc!=null&&paraDesc.trim().length()>0)
						{
						  query.setParameter("paraDesc", "%"+paraDesc+"%");
						}
					   if(paraName!=null&&paraName.trim().length()>0)
						{
						  query.setParameter("paraName", "%"+paraName+"%");
						}
					  List datas=query.list();
						   	getSession().setAttribute(data_session_name,datas);	
					     	writeResponseText("{success:true,msg:'success',datasize:"+datas.size()+"}");
			   			}
			   catch (Exception e) {
				   writeResponseText("{success:false,msg:'failure'}");
					 
			}
			    
			
		}
	 
	 public void excutModifyQuerySysConfigPara()
		{
		 
			String hql="from SysConfigPara where 1=1 and id="+this.id; 
		 	List datas=serviceSupportImp.findByHQL(hql);
		 	SysConfigPara sysConfigPara=(SysConfigPara)datas.get(0);
			try {
				writeResponseText("{success:true,datas:"+JSONObject.fromObject(sysConfigPara).toString()+"}");
		   	} catch (Exception e) {
				try {
					getResponse().getWriter().write("{success:false,msg:'faild',errors:{"+e.getMessage()+"}}");
				} catch (IOException e1) {
				 
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
			
		}
		public void excuteModifySysConfigPara()
		{
	 
			Session session=this.getHibernateSession();
			String hql="from SysConfigPara where 1=1 and id="+this.id; 
			
			List datas=this.getListData(hql, session);
		 
		 	SysConfigPara sysConfigPara=(SysConfigPara)datas.get(0);
		 
		 	SysConfigPara sysConfigPara2=new SysConfigPara(paraName,  paraValue,paraDesc);
			String ignoreProperties[]={"id"};
		 	BeanUtils.copyProperties(sysConfigPara2, sysConfigPara,ignoreProperties);
		 	this.updata(sysConfigPara,session);
		 	getSession().removeAttribute(data_session_name);
		 	addDataToSession();
		 	ModifySevervlettConttextAttribute(session);
		 	session.flush();
		 	session.close();
			writeResponseText("{success:true,msg:'success'}"); 
		}
		public void  ModifySevervlettConttextAttribute(Session session)
		{
			  HashMap<String,String> sysconfigpara=new HashMap<String, String>();
			 String hql3="from SysConfigPara";
			 List datas3=session.createQuery(hql3).list();
			 for(Object o3:datas3)
			 {
				 SysConfigPara sib=(SysConfigPara)o3;
				 sysconfigpara.put(sib.getParaName(), sib.getParaValue());
				 if(sib.getParaName().equals("sendi_system_title")) //系统标题做为默认值放到servletContext中
				 {
					 applicationContextHelper.servletContext.setAttribute("sendi_system_title", sib.getParaValue());
				 }
			 }
			 
			 applicationContextHelper.servletContext.setAttribute("sendi_sys_config_para", sysconfigpara);
			
		}
		
		public void excuteDeleteSysConfigPara()
		{
		  String hql="delete SysConfigPara where id in ("+deleterecord+")";
		  Session session=serviceSupportImp.getDao().getSessionFactory().openSession();
		  session.beginTransaction();
		  session.createQuery(hql).executeUpdate();
		  session.getTransaction().commit();
		  addDataToSession();
		  writeResponseText("{success:true,msg:'success'}"); 
		}
	
	// 重载系统变量 add by chenjunliang 2010-10-25
	public String reloadConfig(){
		try{
			PreloadBeanconfig preloadConfig = (PreloadBeanconfig)applicationContextHelper.appctx.getBean("preloadBeanconfig");
			preloadConfig.reloadConfig();
			writeResponseText("{success:true,msg:'success'}");
		}catch(Exception ex){
			ex.printStackTrace();
			writeResponseText("{success:false,msg:'初始化失败！'}");
		}
		return null;
	}
		
	public Integer getId() {
		return id;
	}


	public void setId(Integer id) {
		this.id = id;
	}


	public String getParaName() {
		return paraName;
	}


	public void setParaName(String paraName) {
		this.paraName = paraName;
	}


	public String getParaValue() {
		return paraValue;
	}


	public void setParaValue(String paraValue) {
		this.paraValue = paraValue;
	}


	public String getParaDesc() {
		return paraDesc;
	}


	public void setParaDesc(String paraDesc) {
		this.paraDesc = paraDesc;
	}


 
 
	

}
